package sys.news.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 字符乱码过滤器:UTF-8
 */
@WebFilter("*.do")//过滤所有的后台请求 *.do
public class CharacterFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("字符编码过滤器启动。。。");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //System.out.println("字符编码过滤器UTF-8。。。");
        HttpServletRequest request = (HttpServletRequest)servletRequest;//转换子类型
        request.setCharacterEncoding("UTF-8");//设置请求编码
        HttpServletResponse response = (HttpServletResponse)servletResponse;//转型子类型
        response.setCharacterEncoding("UTF-8");//设置响应编码
        filterChain.doFilter(request,response);//放行 进入一下后台资源
    }

    @Override
    public void destroy() {

    }
}
